import { mergeOptions, convertColorFormat, jsonToVec3 } from "../core/common.js";
import global from "../core/global.js";

let Polyline3D = {
  add(options, cb) {
    let defaults = {
      geojson: null,
      points: [],
      objectName: "",
      style: {
        mode: "RelativeToTerrain",
        altitude: null,
        color: "#FF0000",
        alpha: 0.8,
        width: 2,
        scale: 1,
        dash: false,
        depthTest: true,
        glow: false,
        glowPower: 0.1,
        animation: null,
      },
    };
    options = mergeOptions(defaults, options);
    if (!options.geojson && options.points.length < 2) {
      throw new TypeError("请传入geojson点数据或大于1个点的points");
    }

    let polyline = null;
    if (options.geojson) {
      polyline = this.jsonRender(options);
    } else {
      let points = jsonToVec3(options.points);
      polyline = this.render(points, options.style);
    }
    polyline.objectName = options.objectName;
    if (options.id) polyline.tag = options.id
    let id = global.Polyline3DStore.add(polyline, options.id);
    cb && cb(id);
    return id;
  },
  jsonRender(options) {
    let geojson = options.geojson;
    let entity = new SSmap.Entity();
    geojson.features.forEach((feature) => {
      let coordinates = feature.geometry.coordinates;
      if (feature.geometry.type == "MultiPath") {
        coordinates.forEach((coord) => {
          let points = jsonToVec3(coord);
          let polyline = Polyline3D.render(points, options.style);
          polyline.parent = entity;
        });
      } else if (feature.geometry.type == "MultiPolygon") {
        coordinates[0].forEach((coord) => {
          let points = jsonToVec3(coord);
          let polyline = Polyline3D.render(points, options.style);
          polyline.parent = entity;
        });
      } else {
        let points = jsonToVec3(coordinates);
        let polyline = Polyline3D.render(points, options.style);
        polyline.parent = entity;
      }
    });
    GlobalViewer.scene.addEntity(entity);
    return entity;
  },
  render(
    points,
    {
      mode,
      altitude,
      width,
      color,
      alpha,
      dash,
      animation,
      depthTest,
      glow,
      glowPower
    }
  ) {
    let polyline = new SSmap.Polyline3D();
    color = convertColorFormat(color);
    polyline.setWidth(width);
    polyline.color = SSmap.Color.fromRgb(color.r, color.g, color.b, 255);
    polyline.alpha = alpha;
    polyline.dash = dash;
    polyline.animation = animation;
    polyline.setGlowMaterial(glow);
    polyline.glowPower = glowPower
    if (animation) {
      let type = animation.type || "HorizontalFlow";
      if (animation.imgUrl) {
        polyline.setImageUrl(animation.imgUrl);
      }
      if (animation.repeat) {
        polyline.setRepeat(animation.repeat);
      }
      polyline.animationRun = animation.running === false ? false : true;
      polyline.animationType = SSmap.AnimationType[type];
      polyline.animationTimer = animation.duration || 2000;
    }
    polyline.depthTest = depthTest;
    // polyline.setMinDistance(5.0); //设定插点的最小距离
    polyline.setAltitudeMethod(SSmap.AltitudeMethod[mode]);
    if (mode == "Absolute" && altitude) {
      polyline.setAltitude(altitude);
    }
    polyline.addProperty("name", "polyline");
    points.forEach((point) => {
      polyline.addPoint(point);
    });
    polyline.draw();
    polyline.end();
    return polyline;
  },
  remove(id) {
    global.Polyline3DStore.has(id) && global.Polyline3DStore.remove(id);
  },
  changeProperty(id, property) {
    let polyline = global.Polyline3DStore.get(id);
    if (polyline) {
      for (const key in property) {
        if (Object.hasOwnProperty.call(property, key)) {
          const value = property[key];
          polyline[key] = value;
          polyline.addProperty(String(key), String(value)); //拾取 属性设置
        }
      }
    }
  },
  returnPolyline3D(id) {
    let polyline = global.Polyline3DStore.get(id);
    return polyline
  }
};

export { Polyline3D };
